{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "lrcghOJOWGHw"
   },
   "source": [
    "# WebsocketServerWorker Tutorial\n",
    "\n",
    "This tutorial is a 2 notebook tutorial. The partner notebook is the notebook entitled SocketWorker Client.ipynb and is in the same folder as this notebook. You should execute this notebook BEFORE the other.\n",
    "\n",
    "In this tutorial, we'll demonstrate how to launch a WebsocketWorker server which will listen for PyTorch commands over a socket connection. In this tutorial, the two workers are connected via a socket connection on the localhost network.\n",
    "\n",
    "If you'd prefer to download this notebook and run it locally on your machine, you can do so via the `Download .ipynb` in the `File` dropdown field in this Google colab environment. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FEltv37gWNVY"
   },
   "source": [
    "\n",
    "# Step -1: Copy This Notebook\n",
    "\n",
    "Go up to File -> Save A Copy in Drive\n",
    "\n",
    "This will let you execute the notebook (it won't let you execute this one by default)\n",
    "\n",
    "# Step 0: Install Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 12362
    },
    "colab_type": "code",
    "id": "k2Ag0M-vWJtS",
    "outputId": "fcca3175-c225-4e83-cad6-b58d68e25497"
   },
   "outputs": [],
   "source": [
    "! git clone https://github.com/OpenMined/PySyft.git\n",
    "! git checkout dev\n",
    "! git pull origin dev\n",
    "# http://pytorch.org/\n",
    "from os.path import exists\n",
    "from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag\n",
    "platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())\n",
    "cuda_output = !ldconfig -p|grep cudart.so|sed -e 's/.*\\.\\([0-9]*\\)\\.\\([0-9]*\\)$/cu\\1\\2/'\n",
    "accelerator = cuda_output[0] if exists('/dev/nvidia0') else 'cpu'\n",
    "\n",
    "#!pip3 install https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-linux_x86_64.whl\n",
    "#!pip3 install https://download.pytorch.org/whl/cu100/torchvision-0.3.0-cp36-cp36m-linux_x86_64.whl\n",
    "\n",
    "import torch\n",
    "\n",
    "!cd PySyft; pip install -r requirements.txt; python setup.py install udacity\n",
    "\n",
    "import os\n",
    "import sys\n",
    "import logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "KuiCRXfn8F7k"
   },
   "outputs": [],
   "source": [
    "print(sys.path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tweak the following to make things work if necessary. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#sys.path.remove('/anaconda3/envs/pysyft/lib/python3.7/site-packages/syft-0.1.21a1-py3.7.egg')\n",
    "#sys.path.append('./PySyft')\n",
    "#print(sys.path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2qq2fs26WGHy"
   },
   "source": [
    "# Step 1: Hook PyTorch\n",
    "\n",
    "Just like previous tutorials, the first step is to override PyTorch commands using the TorchHook object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "M5CgFFBbWGHy"
   },
   "outputs": [],
   "source": [
    "import syft as sy\n",
    "\n",
    "hook = sy.TorchHook(torch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "I7jfZ1GiWGH5"
   },
   "source": [
    "# Step 2: Launch Server\n",
    "\n",
    "The next step is to launch the server. We set the `host` to \"localhost\", `hook` to the \"hook\" set above, `id` to the \"id\" of the worker, and `port` to the worker's \"port\" (in this case the port is at `8182`). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 51
    },
    "colab_type": "code",
    "id": "70gDcOxGWGH6",
    "outputId": "94c3752a-960c-43ee-e6ab-606da6435d7e"
   },
   "outputs": [],
   "source": [
    "from syft.workers.websocket_server import WebsocketServerWorker\n",
    "\n",
    "local_worker = WebsocketServerWorker(\n",
    "                            host=\"localhost\",\n",
    "                            hook=hook,\n",
    "                            id=0,\n",
    "                            port=8182)\n",
    "\n",
    "local_worker.start()  # Might need to interrupt with `CTRL-C` or some other means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 487
    },
    "colab_type": "code",
    "id": "UCQWp2F6WGH9",
    "outputId": "ed98cb4d-41ed-4d24-d7d2-2cce68013207"
   },
   "outputs": [],
   "source": [
    "local_worker.list_objects()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ig_Mgk8YWGH-"
   },
   "outputs": [],
   "source": [
    "local_worker.objects_count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "local_worker.host"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Cf8c9o3ZWGIB"
   },
   "outputs": [],
   "source": [
    "local_worker.port"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "SocketWorker Server.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
